/**
  @file
  @author Stefan Frings
*/

#include <QCoreApplication>
#include "requestmapper.h"
#include "filelogger.h"
#include "staticfilecontroller.h"
#include "mainwindow.h"

RequestMapper::RequestMapper(QObject* parent):m_parent(parent)
//    :HttpRequestHandler(parent)
{
    MainWindow *t;
    t=(MainWindow*)parent;
    qDebug() << t->MwGetContent();
    qDebug("RequestMapper: created");
}


RequestMapper::~RequestMapper()
{
    qDebug("RequestMapper: deleted");
}


void RequestMapper::service(HttpRequest& request, HttpResponse& response)
{
    QByteArray path=request.getPath();
    qDebug("RequestMapper: path=%s",path.data());

    // For the following pathes, each request gets its own new instance of the related controller.

    if (path.startsWith("/dump"))
    {        
        response.setHeader("Content-Type", "text/html; charset=UTF-8");
        response.setCookie(HttpCookie("firstCookie","hello",600,QByteArray(),QByteArray(),QByteArray(),false,true));
        response.setCookie(HttpCookie("secondCookie","world",600));
//        DumpController().service(request, response);
        QByteArray body("<html><body><p><b>Hello World</b></body></html>");
        response.write(body,true);
    }

    if (path.startsWith("/pumd"))
    {
        response.setHeader("Content-Type", "text/html; charset=UTF-8");
//        response.setCookie(HttpCookie("firstCookie","hello",600,QByteArray(),QByteArray(),QByteArray(),false,true));
//        response.setCookie(HttpCookie("secondCookie","world",600));
//        QByteArray body("return context:");
//        QByteArray t = path.mid(5);
//        body += t;
//        QByteArray body("[{\"id\":1,\"name\":\"xiaoming\"},{\"id\":2,\"name\":\"xiaohong\"}]");
        MainWindow *t;
        t=(MainWindow*)m_parent;
        qDebug() <<  t->MwGetContent().toUtf8();
        QByteArray body("handleResponse({\"status\": true, \"user\": \"admin\"})");
//        body = body + "(" + ")";
        response.write(t->MwGetContent().toUtf8(),true);
    }
}
